DEFINE NORMS (!positional = !tokens(1)).
SET MXLOOP = 1000.
DATASET NAME data.
DATASET DECLARE tmp.
OUTPUT NAME current.
OUTPUT NEW.
OMS  select tables
  /destination format = sav outfile = "tmp"
  /if commands = ['frequencies'] subtypes = ['frequencies'].
FREQUENCIES !1.
OMSEND.
DATASET ACTIVATE tmp.
COMPUTE FLTR =(NOT Sysmins(CumulativePercent)).
SELECT IF (FLTR).
compute X = number(Var2, F2). 
OUTPUT CLOSE *.
matrix.
GET n /VARIABLES = Frequency.
GET X /VARIABLES = X.
COMPUTE X = X + 0.000000001.
COMPUTE L = NROW(n).
COMPUTE A1 =transpos( {X, MAKE(L,1,1)}).
COMPUTE A2 = {1, -1}.
COMPUTE A3 = {1}.
COMPUTE MU = A3 * EXP(A2 * LN(A1 * n)).
Compute G= A3*(EXP(A2*(LN(A1*n)))*A2*(INV(MDIAG(A1 * n))*A1*IDENT(L))).
COMPUTE se1 = SQRT(DIAG(G * MDIAG(n) * TRANSPOS(G))).
COMPUTE OU1 = {MU, se1, MU - 1.96 * se1, MU + 1.96 * se1}.
COMPUTE CLABEL1= {'MEAN', 'SE', 'LO', 'UP'}.
COMPUTE X2 = X&*X.
COMPUTE Nobs =CSUM(n).
COMPUTE A1 =transpos( {X, X2, MAKE(L,1,1),MAKE(L,1,1)}).
COMPUTE A2 = {2,0,-1,0;0,1,0,0;0,0,1,0;0,0,1,-1}.
COMPUTE A3 = {-1,1,0,0;0,0,1,-1}.
COMPUTE A4 = {1/2,-1/2}.
COMPUTE A5 = {1}.
COMPUTE Sdev = A5*EXP(A4*LN(A3 * EXP(A2 * LN(A1 * n)))).
COMPUTE G = A5*EXP(A4*LN(A3*EXP(A2*LN(A1*n))))*A4*INV(MDIAG(A3 *EXP(A2*LN(A1*n))))*A3*MDIAG(EXP(A2 *LN(A1*n)))*A2*INV(MDIAG(A1 * n))*A1*IDENT(L).
COMPUTE se2 = SQRT(DIAG(G*MDIAG(n)*TRANSPOS(G)- G*((n*TRANSPOS(n))/Nobs)*TRANSPOS(G))).
COMPUTE OU2 = {Sdev, se2, Sdev - 1.96 * se2, Sdev + 1.96 * se2}.
COMPUTE CLABEL2= {'SD', 'SE', 'LO', 'UP'}.
COMPUTE A1 = TRANSPOS({X,X2, MAKE(L,1,1), MAKE(L,1,1)}).
COMPUTE A2 = {2,0,0,-1;0,1,0,0;0,0,0,1;1,0,0,-1;0,0,1,-1}.
COMPUTE A3 = {-1,1,0,0,0;0,0,1,0,-1;0,0,0,1,0}.
COMPUTE A4 = {1/2,-1/2,0;0,0,1}.
COMPUTE A5 = {-1.75,1;-1.25,1;-.75,1;-.25,1;.25,1;.75,1;1.25,1;1.75,1}.
COMPUTE STB = A5*EXP(A4*LN(A3 * EXP(A2 * LN(A1 * n)))).
COMPUTE G = A5*(MDIAG(EXP(A4*LN(A3*EXP(A2*LN(A1*n)))))*A4*(INV(MDIAG(A3*EXP(A2*LN(A1*n))))*A3*(MDIAG(EXP(A2*LN(A1*n)))*A2*(INV(MDIAG(A1*n))*A1*IDENT(L))))).
COMPUTE se3 = SQRT(DIAG(G*MDIAG(n)*TRANSPOS(G)- G*((n*TRANSPOS(n))/Nobs)*TRANSPOS(G))).
COMPUTE OU3 = {STB, se3, STB - 1.96 * se3, STB + 1.96 * se3}.
COMPUTE CLABEL3= {'ST_B', 'SE', 'LO', 'UP'}.
COMPUTE RLABEL3 = {'1-2','2-3','3-4','4-5','5-6','6-7','7-8','8-9'}.
COMPUTE J = MAKE(L,L,1e-12).
LOOP ii =1 TO L.
+ LOOP jj =1 TO L.
+   DO IF (jj GE ii).
+     COMPUTE J(ii,jj) = 1.
+   END IF.
+ END LOOP.
END LOOP.
COMPUTE K = MAKE(L,L,1e-12).
LOOP ii =1 TO L.
+ LOOP jj =1 TO L.
+   DO IF (jj EQ ii OR jj EQ ii-1).
+     COMPUTE K(ii,jj) = 50.
+   END IF.
+ END LOOP.
END LOOP.
COMPUTE A1 =transpos({J, MAKE(L,1,1)}).
COMPUTE A2 ={IDENT(L),MAKE(L,1,-1)}.
COMPUTE A3 = K .
COMPUTE PR = A3 * EXP(A2 * LN(A1 * n)).
compute G = A3*(MDIAG(EXP(A2 *LN(A1*n)))*A2*(INV(MDIAG(A1 * n))*A1*IDENT(L))).
COMPUTE se4 = SQRT(DIAG(G*MDIAG(n)*TRANSPOS(G)- G*((n*TRANSPOS(n))/Nobs)*TRANSPOS(G))).
COMPUTE OU4 = {X, PR, se4, PR - 1.96 * se4, PR + 1.96 * se4}.
COMPUTE CLABEL4= {'X', 'P_RANK', 'SE', 'LO', 'UP'}.
COMPUTE A1 = TRANSPOS({IDENT(L), MAKE(L,1,1), MAKE(L,1,1)}).
COMPUTE A2 = {IDENT(L),MAKE(L,1,0),MAKE(L,1,0);MAKE(1,L,0),1,-1}. 
COMPUTE A3 = {T(X),0;T(X2),0;MAKE(1,L,1),0; MAKE(1,L,1),0;MAKE(L,L,0),X}.
COMPUTE A4 = {1,0,0,-1, MAKE(1,L,0);2,0,0,-1,MAKE(1,L,0);0,1,0,0,MAKE(1,L,0);0,0,0,1,MAKE(1,L,0);0,0,1,-1,MAKE(1,L,0);MAKE(L,4,0),IDENT(L)}.
COMPUTE A5 = {1,MAKE(1,4+L,0);0,-1,1,MAKE(1,L+2,0);0,0,0,1,-1,MAKE(1,L,0);MAKE(L,5,0),IDENT(L)}.
COMPUTE A6 = {1,-.5,.5, MAKE(1,L,0);MAKE(L,1,0),MAKE(L,1,-.5),MAKE(L,1,.5),IDENT(L)}.
COMPUTE A7 = {MAKE(L,1,-1),IDENT(L)}.
COMPUTE ZX1 = EXP(A6*LN(A5*EXP(A4*LN(A3*EXP(A2*LN(A1*n)))))).
COMPUTE ZX= A7*ZX1.
COMPUTE g_1 = LN(A1*n).
COMPUTE g_2 = EXP(A2*g_1).
COMPUTE g_3 = LN(A3*g_2).
COMPUTE g_4 = EXP(A4*g_3).
COMPUTE g_5 = LN(A5*g_4).
COMPUTE g_6= EXP(A6*g_5).
COMPUTE g_7 = A7*g_6.
COMPUTE G0 = IDENT(L).
COMPUTE G1 = INV(MDIAG(A1*n))*A1*G0.
COMPUTE G2 = MDIAG(EXP(A2*g_1))*A2*G1.
COMPUTE G3 = INV(MDIAG(A3*g_2))*A3*G2.
COMPUTE G4 = MDIAG(EXP(A4*g_3))*A4*G3.
COMPUTE G5 = INV(MDIAG(A5*g_4))*A5*G4.
COMPUTE G6 = MDIAG(EXP(A6*g_5))*A6*G5.
COMPUTE G = A7 * G6.
COMPUTE se5 = SQRT(DIAG(G*MDIAG(n)*TRANSPOS(G)- G*((n*TRANSPOS(n))/Nobs)*TRANSPOS(G))).
COMPUTE OU5 = {X, ZX, se5, ZX - 1.96 * se5, ZX + 1.96 * se5}.
COMPUTE CLABEL5= {'X', 'Z_X', 'SE', 'LO', 'UP'}.
Print OU1 /FORMATS = F8.3 /CNAMES = CLABEL1 / TITLE 'MEAN SCORE'.
Print OU2 /FORMATS = F8.3 /CNAMES = CLABEL2 / TITLE 'STANDARD DEVIATION'.
Print OU4 /FORMATS = F8.3  /CNAMES = CLABEL4 / TITLE 'PERCENTILE RANK SCORES'.
Print OU3 /FORMATS = F8.3 /RNAMES = RLABEL3 /CNAMES = CLABEL3 / TITLE 'STANINE BOUNDARIES'.
Print OU5 /FORMATS = F8.3  /CNAMES = CLABEL5 / TITLE 'STANDARDIZED SCORES'.
end matrix.
DATASET ACTIVATE data.
DATASET CLOSE tmp.
!ENDDEFINE.

NORMS X.
NORMS Y.